package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.BillState;
import com.ljxt.dt.domain.HandlerResult;
import com.ljxt.dt.domain.HandlerType;
import com.ljxt.dt.domain.ReceptType;
import com.ljxt.dt.service.storemanagementservice.StoreService;
import com.ljxt.dt.service.storemanagementservice.impl.StoreServiceImpl;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.FindVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-11 14:06
 * @copyright 老九学堂
 */
@WebServlet("/store.html")
public class Store extends HttpServlet {
    private StoreService admin = new StoreServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BillState> name =  admin.selectResult();

        List<ReceptType>  result = admin.selectType();
       req.setAttribute("name",name);
       req.setAttribute("result",result);
        ServletUtil.forward(req,resp,"store","storeManagement");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageIndex = ServletUtil.getQueryInt(req,"pageIndex");
        Integer pageSize = ServletUtil.getQueryInt(req,"pageSize");
        if(pageIndex == 0){
            pageIndex = 1;
        }
        if(pageSize == 0){
            pageSize = 5;
        }
        FindVO findVO = new FindVO();
        findVO = ServletUtil.toObj(req,findVO);

        PageVO<ReceptVO> pageVO =  admin.findAll(pageIndex,pageSize,findVO);
        JsonResult<PageVO> jsonResult = new JsonResult<>();
        pageVO.setPageURL("store.html");
        jsonResult.setData(pageVO);
        ObjectMapper objectMapper = new ObjectMapper();

        resp.getWriter().print(objectMapper.writeValueAsString(jsonResult));

    }
}
